﻿using System;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using Microsoft.Win32;
using VirusCleaner.IPlugin;
using VirusCleaner.IPlugin.Plugin;

namespace VirusCleaner.Service
{
    public partial class ServicesControl : UserControl,IUiElement
    {
        public ServicesControl()
        {
            InitializeComponent();
        }
        #region IUiElement 成员
        public string PluginName
        {
            get { return ""; }
        }
        public int Init()
        {
            GetAllServices();
            timerService.Start();
            return 0;
        }

        public void Active(bool active)
        {
            timerService.Enabled = active;
        }

        #endregion
        private void ServicesControl_Load(object sender, EventArgs e)
        {
            
        }
        public void GetAllServices()
        {
            ServiceController[] services = ServiceController.GetServices();//检索计算机上的所有服务;
            foreach (ServiceController service in services)//将服务的名称添加到listView1中
            {
                var item = cListViewServices.FindItemWithText(service.ServiceName);
                if (item != null)
                {
                    item.SubItems[4].Text = ServiceStatus(service.Status);
                }
                else
                {
                    item = new ListViewItem(service.ServiceName);
                    item.SubItems.Add(service.DisplayName);//服务的友好名称
                    item.SubItems.Add(Registry.GetValue(string.Format(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\{0}", service.ServiceName), "Description", "").ToString());
                    item.SubItems.Add(ServiceType(service.ServiceType));
                    item.SubItems.Add(ServiceStatus(service.Status));
                    item.SubItems.Add(ServiceStart(Convert.ToInt32(Registry.GetValue(string.Format(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\{0}", service.ServiceName), "Start", ""))));
                    item.SubItems.Add(ServiceObject(Registry.GetValue(string.Format(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\{0}", service.ServiceName), "ObjectName", "").ToString()));
                    item.SubItems.Add(Registry.GetValue(string.Format(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\{0}", service.ServiceName), "ImagePath", "").ToString());
                    cListViewServices.Items.Add(item);
                }
            }
        }
        private static string ServiceType(ServiceType type)
        {
            switch (type)
            {
                case System.ServiceProcess.ServiceType.Adapter:
                    return "硬件适配器";
                case System.ServiceProcess.ServiceType.FileSystemDriver:
                    return "文件系统驱动";
                case System.ServiceProcess.ServiceType.InteractiveProcess:
                    return "共享进程";
                case System.ServiceProcess.ServiceType.KernelDriver:
                    return "系统内核驱动";
                case System.ServiceProcess.ServiceType.RecognizerDriver:
                    return "识别驱动";
                case System.ServiceProcess.ServiceType.Win32OwnProcess:
                    return "独立进程";
                case System.ServiceProcess.ServiceType.Win32ShareProcess:
                    return "共享进程";

            }
            return type.ToString();
        }
        private static string ServiceStatus(ServiceControllerStatus status)
        {
            switch (status)
            {
                case ServiceControllerStatus.Running:
                    return "正在运行";
                case ServiceControllerStatus.Paused:
                    return "暂停运行";
                case ServiceControllerStatus.Stopped:
                    return "停止运行";
                case ServiceControllerStatus.PausePending:
                    return "准备暂停";
                case ServiceControllerStatus.StartPending:
                    return "准备开始";
                case ServiceControllerStatus.ContinuePending:
                    return "准备继续";
                case ServiceControllerStatus.StopPending:
                    return "准备停止";
            }
            return "未知状态";
        }
        private static string ServiceStart(int startType)
        {
            switch (startType)
            {
                case -1:
                    return "错误";
                case 0:
                    return "启动";
                case 1:
                    return "系统";
                case 2:
                    return "自动";
                case 3:
                    return "手动";
                case 4:
                    return "已禁用";
            }
            return "未知";
        }
        private static string ServiceObject(string objectName)
        {
            switch (objectName.ToUpper())
            {
                case "NT AUTHORITY\\LOCALSERVICE":
                    return "本地服务";
                case "NT AUTHORITY\\NETWORKSERVICE":
                    return "网络服务";
                case "LOCALSYSTEM":
                    return "本地系统";
            }
            return objectName;
        }

        private void TimerServiceTick(object sender, EventArgs e)
        {
            GetAllServices();
        }

    }
}
